System calls.html (6488B)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html><head><link rel="stylesheet" href="sitewide.css"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="exporter-version" content="Evernote Mac 7.6 (457297)"/><meta name="altitude" content="-1.414480566978455"/><meta name="author" content="Alex Balgavy"/><meta name="created" content="2018-11-08 08:11:28 +0000"/><meta name="latitude" content="52.33346557617188"/><meta name="longitude" content="4.866784463121516"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2018-11-09 12:35:16 +0000"/><title>System calls</title></head><body><ul><li><div>every process starts with 3 files open: stdin, stdout, stderr</div></li><li><div>steps:</div></li></ul><div><img src="System%20calls.resources/96F6E180-A92F-461F-B610-A089729C2D01.png" height="745" width="959"/></div><ul><li><div>what has to happen to print hello world to stdout?</div></li><ul><li><div>build process:</div></li></ul><div style="margin-left: 40px;"><img src="System%20calls.resources/67E58FA0-18D7-41DC-95D6-341B68F15454.png" height="615" width="546"/></div><ul><li><div>iteration 1 4 </div></li></ul></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div>#include <stdio.h></div><div> int main(int argc, char **argv) {</div><div> printf("Hello World!\n");</div><div> return 0; </div><div> }</div></div><div><br/></div><ul><ul><li><div>iteration 2 5 </div><div><br/></div></li></ul></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div>#include <unistd.h></div><div>#define STDOUT 1</div><div>int main(int argc, char **argv) {</div><div><span> </span>char msg[] = "Hello World!\n";</div><div><span> </span>write(STDOUT, msg, sizeof(msg));</div><div> return 0;</div><div>}</div></div><div><br/></div><ul><ul><li><div>iteration 3 6 </div><div><br/></div></li></ul></ul><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div>#define _GNU_SOURCE</div><div>#include <sys/syscall.h></div><div>#define STDOUT 1</div><div>int main(int argc, char **argv) {</div><div><span> </span>char msg[] = "Hello World!\n”;</div><div><span> </span>int nr = SYS_write;</div><div><span> </span>syscall(nr, STDOUT, msg, sizeof(msg));</div><div><span> </span>return 0;</div><div>}</div></div><div><br/></div><ul><li><div>syscall diagram</div></li></ul><div style="margin-left: 40px;"><img src="System%20calls.resources/F197EB04-48B5-4FB8-9784-9576A5E5A442.png" height="526" width="625"/></div><ul><li><div>syscall (x86 Linux) is triggered by instruction (like 0x80):</div></li><ul><li><div>privilege level changed to kernel mode</div></li><li><div>program counter set to specific location</div></li><li><div>arguments passed in registers: 7 </div></li><ul><li><div>rax <- syscall number</div></li><li><div>ebx, ecdx, edx, esi, edi, ebp <- arguments</div></li><li><div>stack <- more arguments</div></li></ul><li><div>x86-64 supports legacy int 0x80, new instruction syscall 8 </div></li><ul><li><div>rax <- syscall number (different from 32bit)</div></li><li><div>rdi, rsi, rdx, r10, r8, r9 <- arguments</div></li></ul></ul><li><div>hello world without glibc -- manual system calls, in-line assembly:</div></li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div>ssize_t write(int fd, const void *buf, size_t nbytes) {</div><div> ssize_t ret;</div><div> asm volatile</div><div> (</div><div><span> <span> <span> <span> </span></span></span></span>/* request syscall to OS (can also be ‘int $0x80’) */</div><div> “syscall”</div><div> </div><div><span> <span> <span> <span> /* return result in %eax */</span></span></span></span><br/></div><div><span><span><span/></span></span><span> <span> <span> <span> </span></span></span></span>: "=a" (ret)</div><div> </div><div><span> <span> <span> <span> </span></span></span></span>/* __NR_write (1) into same place as operand 0, fd into %rdi, buffer into %rsi, length into %rdx */</div><div><span> <span> <span> <span> </span></span></span></span>: "0" (__NR_write), "D"(fd), "S"(buf), "d"(nbytes)</div><div><br/></div><div><span> <span> <span> <span> /* modified cc, registers %rcx and %r11, and memory */</span></span></span></span><br/></div><div> : "cc", "rcx", "r11", "memory"</div><div> );</div><div> return ret;</div><div> }</div></div><div><br/></div><ul><ul><li><div>actual objdump of this program</div></li></ul></ul><div><img src="System%20calls.resources/5724BA52-D9D9-4370-BDCC-004143484C9C.png" height="288" width="582"/></div><div><br/></div></body></html>